package com.sky.yipinyoufujava.controller;

import com.sky.yipinyoufujava.pojo.User;
import com.sky.yipinyoufujava.pojo.from.ShopsFrom;
import com.sky.yipinyoufujava.pojo.goods.Shops;
import com.sky.yipinyoufujava.pojo.goods.ShopsUserInfo;
import com.sky.yipinyoufujava.result.Result;
import com.sky.yipinyoufujava.service.LoginService;
import com.sky.yipinyoufujava.service.ShopsService;
import com.sky.yipinyoufujava.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@Slf4j
public class ShopsConteroller {
    @Autowired
    private ShopsService shopsService;
    @Autowired
    private LoginService loginService;
    //    获取所有的店铺信息
    @PostMapping("/getAllShop")
    private Result getAllShop(@RequestBody ShopsFrom shopsFrom){
        log.info("获取所有的店铺信息");
        List<Shops> shopsList = shopsService.getAllShop(shopsFrom);
//        循环 shopsList 给里面的ShopsUserInfo 添加name和phone
        for (Shops shops : shopsList) {
            User user = loginService.queryUserId(shops.getShopsUserId());
            ShopsUserInfo shopsUserInfo = new ShopsUserInfo();
            shopsUserInfo.setUserName(user.getUsername());
            shopsUserInfo.setPhone(user.getPhone());
            shops.setShopsUserInfo(shopsUserInfo);
        }
        return Result.success(shopsList);
    };

    // 添加店铺信息
    @PostMapping("/addShop")
    private Result addShop(@RequestBody Shops shops ,@RequestHeader("Token") String token){
        log.info("添加店铺信息,{}",shops);
        Map<String, Object> stringObjectMap = JwtUtil.parseToken(token);
        Object UserId = stringObjectMap.get("id");
        log.info("添加的用户id,{}",UserId);
        shops.setShopsUserId(UserId.toString());
        shopsService.addShop(shops);
        return Result.success("添加成功！");
    };

    // 更新店铺的信息
    @PostMapping("/updateShop")
    private Result updateShop(@RequestBody Shops shops){
        log.info("更新店铺的信息,{}",shops);
        shopsService.updateShop(shops);
        return Result.success("更新成功！");
    };

    // 删除店铺的信息
    @PostMapping("/deleteShop")
    private Result deleteShop(@RequestBody List<Integer> ids){
        log.info("删除店铺的信息,{}",ids);
        shopsService.deleteShop(ids);
        return Result.success("删除成功！");
    };


    // 只获取用户所创建的店铺
    @PostMapping("/getShopsInfo")
    private Result getShopsInfo(@RequestHeader("Token") String token){
        Map<String, Object> stringObjectMap = JwtUtil.parseToken(token);
        Object UserId = stringObjectMap.get("id");
        log.info("添加的用户id,{}",UserId);
        Shops shops =  shopsService.getShopByUserId(UserId);
        User user = loginService.queryUserId(UserId);
        ShopsUserInfo shopsUserInfo = new ShopsUserInfo();
        shopsUserInfo.setUserName(user.getUsername()); // 假设 User 类中有一个 getUsername 方法
        shopsUserInfo.setPhone(user.getPhone()); // 假设 User 类中有一个 getUsername 方法
        shops.setShopsUserInfo(shopsUserInfo);
        return Result.success(shops);
    }
}
